package com.mobi.Survey.lib.uicomponents;

import java.util.StringTokenizer;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.mobi.Survey.lib.model.SurveyDetailPojo;
import com.mobi.Survey.lib.utils.Helper;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * CustomTimePicker.java
 * 
 * 
 */
public class CustomTimePicker extends TimePicker
{
	public static final String DELIM = "|";
	private LinearLayout ll;
	private Context ctx;

	public CustomTimePicker(Context context, SurveyDetailPojo sdp,
			boolean editable)
	{

		super(context);
		this.ctx = context;
		this.setIs24HourView(Boolean.FALSE);
		ll = new LinearLayout(ctx);

		ll.setOrientation(LinearLayout.VERTICAL);
		ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		if (sdp.getControlText() != null && sdp.getControlText().length() > 0)
		{
			CustomTextView txt = new CustomTextView(ctx);
			txt.setCustomColor(sdp);
			txt.setText(sdp.getControlText());
			ll.addView(txt);

		}

		if (sdp.getUserValue().length() > 0)
		{

			try
			{

				StringTokenizer st = new StringTokenizer(sdp.getUserValue(),
						DELIM);
				String hours = st.nextToken();
				String mins = st.nextToken();
				this.setCurrentHour(Helper.toInt(hours));
				this.setCurrentMinute(Helper.toInt(mins));

			} catch (Exception e)
			{
				e.printStackTrace();
			}

		} else if (sdp.getDefaultValue().length() > 0)
		{
			try
			{
				StringTokenizer st = new StringTokenizer(sdp.getDefaultValue(),
						DELIM);
				String hours = st.nextToken();
				String mins = st.nextToken();

				this.setCurrentHour(Helper.toInt(hours));
				this.setCurrentMinute(Helper.toInt(mins));

			} catch (Exception e)
			{
				e.printStackTrace();
			}

		}
		if (!editable)
			this.setEnabled(false);
		ll.addView(this);
	}

	void applyFontRecursively(ViewGroup parent, SurveyDetailPojo sdp)
	{
		for (int i = 0; i < parent.getChildCount(); i++)
		{
			View child = parent.getChildAt(i);
			if (child instanceof ViewGroup)
			{
				if (null != sdp && null != sdp.getSurvey()
						&& null != sdp.getSurvey().getFontColor())
				{

					applyFontRecursively((ViewGroup) child, sdp);
				}
			} else if (child != null)
			{

				if (child instanceof TextView)
				{
					TextView textView = (TextView) child;
					textView.setTextColor(Color.parseColor("#"
							+ sdp.getSurvey().getFontColor()));

				}
			}
		}
	}

	public View getView()
	{
		return ll;
	}

}
